//
//  XGL_PersonalHomePageView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/17.
//

import UIKit

protocol PersonalDelegate: NSObjectProtocol {
    func userFans()
    func userFollow()
    func userGifts()
    func store()
}

class XGL_PersonalHomePageView: UIView{
    weak var delegate: PersonalDelegate?
    var inforView: XGL_UserDataView!
    var gamesView: XGL_PersonalGamesView!
    var bottomView: XGL_PersonalBottomView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createXglView()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension XGL_PersonalHomePageView{
    @objc private func fansItemAction(){
        self.delegate?.userFans()
    }
    @objc private func followItemAction(){
        self.delegate?.userFollow()
    }
    @objc private func giftsItemAction(){
        self.delegate?.userGifts()
    }
    @objc private func storeItemAction(){
        self.delegate?.store()
    }
}

extension XGL_PersonalHomePageView{
    private func createXglView(){
        let scrollView = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.showsVerticalScrollIndicator = false
        self.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        let contentView = UIView()
        scrollView.addSubview(contentView)
        contentView.snp.makeConstraints { (make) in
            make.edges.equalTo(scrollView)
            make.width.equalTo(scrollView)
        }
        self.inforView = XGL_UserDataView().then({ make in
            make.fansItem.itemButton.addTarget(self, action: #selector(self.fansItemAction), for: .touchUpInside)
            make.followItem.itemButton.addTarget(self, action: #selector(self.followItemAction), for: .touchUpInside)
            make.giftsItem.itemButton.addTarget(self, action: #selector(self.giftsItemAction), for: .touchUpInside)
        })
        let storeButton = UIButton().then { make in
            make.setImage(imageWithName("icon_xgl_store"), for: .normal)
            make.addTarget(self, action: #selector(self.storeItemAction), for: .touchUpInside)
        }
        self.gamesView = XGL_PersonalGamesView()
        self.bottomView = XGL_PersonalBottomView(titles: ["My Post","Liked"])
        contentView.addSubviews([inforView,storeButton,gamesView,bottomView])
       
        inforView.snp.makeConstraints { make in
            make.width.centerX.top.equalToSuperview()
        }
        storeButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.left.equalTo(15.0)
            make.height.equalTo(storeButton.snp.width).multipliedBy(83.0/345.0)
            make.top.equalTo(inforView.snp.bottom).offset(24.0)
        }
        gamesView.snp.makeConstraints { make in
            make.centerX.width.equalToSuperview()
            make.top.equalTo(storeButton.snp.bottom).offset(24.0)
        }
        bottomView.snp.makeConstraints { make in
            make.centerX.width.equalToSuperview()
            make.top.equalTo(gamesView.snp.bottom).offset(24.0)
            make.height.equalTo(conversionWithScrennWidth(value: 450.0))
        }
        contentView.snp.makeConstraints { make in
            make.bottom.equalTo(bottomView)
        }
    }
}
